﻿Imports CDDBCONTROLLib
Imports System.IO
Imports System.String

'Copyright 2013 Brendan K Callahan/Callahan Consulting

Public Class MainActivity
    Dim strSeperator As String = " - "
    Dim strFileSeperator As String = " - "
    Dim intFileSepLength As Integer = 3

    'Load Event
    Private Sub MainActivity_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Load events
        cmbFields.SelectedIndex = 1
        cmbSeperator.SelectedIndex = 0
        cmbFileSeperator.SelectedIndex = 0
        rad1Artist.Checked = True
        rad2Title.Checked = True
        rad3Album.Checked = True
        rad4Track.Checked = True
        rad5BPM.Checked = True
        cmbPosition1.SelectedIndex = 0
        cmbPosition2.SelectedIndex = 2
        cmbPosition3.SelectedIndex = 3
        radChangeFile.Checked = True
        grp1Position.Enabled = True
        grp2Position.Enabled = True
        grp3Position.Enabled = False
        grp4Position.Enabled = False
        grp5Position.Enabled = False
        lblPreview.Text = Nothing
        grpID3ToFile.Enabled = True
        grpFileToID3.Enabled = False
        radSpaceTo_.Enabled = False
        rad_ToSpace.Enabled = False
        rad_ToSpace.Checked = True
        chkReplaceSpace.Checked = False
    End Sub

    'Button Events
    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim This As New CddbID3Tag
        This = AssembleID3()
        SaveData(txtFilePath.Text, This)
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        ClearFields()
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click
        Dim FileOpen As New OpenFileDialog
        Dim This As CddbID3Tag
        FileOpen.Filter = "MPEG Layer 3 Files (*.mp3)|*.mp3|" _
             & "All Files(*.*)|"
        FileOpen.FilterIndex = 0
        If FileOpen.ShowDialog() = DialogResult.OK Then
            txtFilePath.Text = FileOpen.FileName
            This = LoadData(txtFilePath.Text)
        Else
            txtFilePath.Text = "Error"
        End If
    End Sub

    Private Sub btnDoEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnDoEdit.Click
        Dim Tokens As Integer = TokenCount()
        Dim strFilePath As String = txtFilePath.Text

        'Change file name based on ID3
        If radChangeFile.Checked = True Then
            LoadData(txtFilePath.Text)
            My.Computer.FileSystem.RenameFile(txtFilePath.Text, SetNewFileName())
        End If

        'Change ID3 based on file name
        If radChangeID3.Checked = True Then
            Dim This As New CddbID3Tag
            If chkWipeID3.Checked = False Then
                'Load existing data
                This = LoadData(txtFilePath.Text)
            Else
                'Clear
                ClearFields()
                This = AssembleID3()
            End If


            If InStr(strFilePath, strFileSeperator) <> 0 Then
                Dim strNewString As String = Nothing
                Dim strTokenString As String = Nothing
                Dim strSubstitution As String = Nothing
                'Trim extension & path
                strNewString = Microsoft.VisualBasic.Strings.Left(strFilePath, (Strings.Len(strFilePath) - 4))
                strNewString = Microsoft.VisualBasic.Strings.Right(strNewString, (InStr(StrReverse(strNewString), "\")) - 1)
                If chkReplaceSpace.Checked = True Then
                    If rad_ToSpace.Checked = True Then
                        strSubstitution = " "
                    Else
                        strSubstitution = "_"
                    End If
                Else
                    strSubstitution = ""
                End If

                If Tokens >= 1 Then
                    SetToken(strNewString, strTokenString, strFileSeperator, intFileSepLength, strSubstitution)
                    Set1Position(This, strTokenString)
                End If
                If Tokens >= 2 Then
                    SetToken(strNewString, strTokenString, strFileSeperator, intFileSepLength, strSubstitution)
                    Set2Position(This, strTokenString)
                End If
                If Tokens >= 3 Then
                    SetToken(strNewString, strTokenString, strFileSeperator, intFileSepLength, strSubstitution)
                    Set3Position(This, strTokenString)
                End If
                If Tokens >= 4 Then
                    SetToken(strNewString, strTokenString, strFileSeperator, intFileSepLength, strSubstitution)
                    Set4Position(This, strTokenString)
                End If
                If Tokens = 5 Then
                    SetToken(strNewString, strTokenString, strFileSeperator, intFileSepLength, strSubstitution)
                    Set5Position(This, strTokenString)
                End If
                SaveData(txtFilePath.Text, This)
            Else
                ' "Seperator not found, please try again"
            End If
        End If
    End Sub


    'General Subs & Functions

    Private Sub SetToken(ByRef strNewString As String,
                              ByRef strTokenString As String,
                              ByVal strFileSeperator As String,
                              ByVal intFileSepLength As Integer,
                              ByVal strReplace As String)
        If InStr(strNewString, strFileSeperator) > 0 Then
            strTokenString = Microsoft.VisualBasic.Strings.Left(strNewString, (InStr(strNewString, strFileSeperator) - (intFileSepLength - 2)))
            strNewString = Microsoft.VisualBasic.Strings.Replace(strNewString, (strTokenString + strFileSeperator), "", 1)
        Else
            strTokenString = strNewString
        End If
        If strReplace <> "" Then
            If strReplace = " " Then
                ReplaceSpace(strTokenString)
            Else
                Replace_(strTokenString)
            End If
        End If
    End Sub

    Private Sub ReplaceSpace(ByRef This As String)
        While Strings.InStr(This, " ") > 0
            Strings.Replace(This, " ", "_")
        End While
    End Sub

    Private Sub Replace_(ByRef This As String)
        While Strings.InStr(This, "_") > 0
            Strings.Replace(This, "_", " ")
        End While
    End Sub

    Private Sub SaveData(ByVal Here As String, ByVal This As CddbID3Tag)
        This.SaveToFile(Here)
    End Sub

    Private Sub PopulateForm(ByVal This As CddbID3Tag)
        'Populate fields on form
        txtSongTitle.Text = This.Title
        txtAlbum.Text = This.Album
        txtYear.Text = This.Year
        txtTrack.Text = This.TrackPosition
        txtBPM.Text = This.BeatsPerMinute
        txtLeadArtists.Text = This.LeadArtist
        txtCopyrightYear.Text = This.CopyrightYear
        txtCopyrightHolder.Text = This.CopyrightHolder
        txtGenre.Text = This.Genre
        txtFileID.Text = This.FileId
        txtPartOfSet.Text = This.PartOfSet
        txtMovie.Text = This.Movie
        txtComments.Text = This.Comments
    End Sub

    Private Sub ClearFields()
        txtSongTitle.Text = Nothing
        txtAlbum.Text = Nothing
        txtYear.Text = Nothing
        txtTrack.Text = Nothing
        txtBPM.Text = Nothing
        txtLeadArtists.Text = Nothing
        txtCopyrightYear.Text = Nothing
        txtCopyrightHolder.Text = Nothing
        txtGenre.Text = Nothing
        txtFileID.Text = Nothing
        txtPartOfSet.Text = Nothing
        txtMovie.Text = Nothing
        txtComments.Text = Nothing
    End Sub

    Private Function SetNewFileName() As String
        Dim This As New CddbID3Tag
        Dim strNewFileName As String = Nothing
        If txtFilePath.Text <> "" Then
            This = LoadData(txtFilePath.Text)
            If cmbFields.SelectedIndex >= 0 Then
                strNewFileName = strNewFileName + GetPosition1Value(This)
            End If
            If cmbFields.SelectedIndex >= 1 Then
                strNewFileName = strNewFileName + strSeperator + GetPosition2Value(This)
            End If
            If cmbFields.SelectedIndex >= 2 Then
                strNewFileName = strNewFileName + strSeperator + GetPosition3Value(This)
            End If
            If cmbFields.SelectedIndex >= 3 Then
                strNewFileName = strNewFileName + strSeperator + GetPosition4Value(This)
            End If
            If cmbFields.SelectedIndex >= 4 Then
                strNewFileName = strNewFileName + strSeperator + GetPosition5Value(This)
            End If
            strNewFileName = strNewFileName + ".mp3"
            lblPreview.Text = strNewFileName
            lblPreview.Visible = True

        Else
            'Insert error message - file name can't be blank
        End If
        Return (strNewFileName)
    End Function

    Private Function AssembleID3()
        Dim This As New CddbID3Tag
        'Populate from fields on form
        This.Title = txtSongTitle.Text
        This.Album = txtAlbum.Text
        This.Year = txtYear.Text
        This.TrackPosition = txtTrack.Text
        This.BeatsPerMinute = txtBPM.Text
        This.LeadArtist = txtLeadArtists.Text
        This.CopyrightYear = txtCopyrightYear.Text
        This.CopyrightHolder = txtCopyrightHolder.Text
        This.Genre = txtGenre.Text
        This.FileId = txtFileID.Text
        This.PartOfSet = txtPartOfSet.Text()
        This.Movie = txtMovie.Text
        This.Comments = txtComments.Text
        Return (This)
    End Function

    Private Function LoadData(ByVal l_strFileName As String) As CddbID3Tag
        'Get ID 3 Object from file
        Dim This As New CddbID3Tag
        This.LoadFromFile(l_strFileName, False)
        This.SaveToFile(l_strFileName)
        This.LoadFromFile(l_strFileName, False)
        PopulateForm(This)
        Return (This)
    End Function

    Private Function GetPosition1Value(ByVal That As CddbID3Tag) As String
        Dim This As String = Nothing
        If rad1Album.Checked = True Then
            This = That.Album
        End If
        If rad1Artist.Checked = True Then
            This = That.LeadArtist
        End If
        If rad1BPM.Checked = True Then
            This = That.BeatsPerMinute
        End If
        If rad1Title.Checked = True Then
            This = That.Title
        End If
        If rad1Track.Checked = True Then
            This = That.TrackPosition
        End If
        If rad1Year.Checked = True Then
            This = That.Year
        End If
        Return (This)
    End Function

    Private Function GetPosition2Value(ByVal That As CddbID3Tag) As String
        Dim This As String = Nothing
        If rad2Album.Checked = True Then
            This = That.Album
        End If
        If rad2Artist.Checked = True Then
            This = That.LeadArtist
        End If
        If rad2BPM.Checked = True Then
            This = That.BeatsPerMinute
        End If
        If rad2Title.Checked = True Then
            This = That.Title
        End If
        If rad2Track.Checked = True Then
            This = That.TrackPosition
        End If
        If rad2Year.Checked = True Then
            This = That.Year
        End If
        Return (This)
    End Function

    Private Function GetPosition3Value(ByVal That As CddbID3Tag) As String
        Dim This As String = Nothing
        If rad3Album.Checked = True Then
            This = That.Album
        End If
        If rad3Artist.Checked = True Then
            This = That.LeadArtist
        End If
        If rad3BPM.Checked = True Then
            This = That.BeatsPerMinute
        End If
        If rad3Title.Checked = True Then
            This = That.Title
        End If
        If rad3Track.Checked = True Then
            This = That.TrackPosition
        End If
        If rad3Year.Checked = True Then
            This = That.Year
        End If
        Return (This)
    End Function

    Private Function GetPosition4Value(ByVal That As CddbID3Tag) As String
        Dim This As String = Nothing
        If rad4Album.Checked = True Then
            This = That.Album
        End If
        If rad4Artist.Checked = True Then
            This = That.LeadArtist
        End If
        If Rad4BPM.Checked = True Then
            This = That.BeatsPerMinute
        End If
        If rad4Title.Checked = True Then
            This = That.Title
        End If
        If rad4Track.Checked = True Then
            This = That.TrackPosition
        End If
        If rad4Year.Checked = True Then
            This = That.Year
        End If
        Return (This)
    End Function

    Private Function GetPosition5Value(ByVal That As CddbID3Tag) As String
        Dim This As String = Nothing
        If rad5Album.Checked = True Then
            This = That.Album
        End If
        If rad5Artist.Checked = True Then
            This = That.LeadArtist
        End If
        If rad5BPM.Checked = True Then
            This = That.BeatsPerMinute
        End If
        If rad5Title.Checked = True Then
            This = That.Title
        End If
        If rad5Track.Checked = True Then
            This = That.TrackPosition
        End If
        If Rad5Year.Checked = True Then
            This = That.Year
        End If
        Return (This)
    End Function

    Private Function TokenCount() As Integer
        'Determine how many tokens we're pulling from file name
        Dim This As Integer
        If cmbPosition2.SelectedIndex > 0 Then
            If cmbPosition3.SelectedIndex > 0 Then
                If cmbPosition4.SelectedIndex > 0 Then
                    If cmbPosition5.SelectedIndex > 0 Then
                        This = 5
                    Else
                        This = 4
                    End If
                Else
                    This = 3
                End If
            Else
                This = 2
            End If
        Else
            This = 1
        End If
        Return (This)
    End Function

    Private Sub Set1Position(ByRef This As CddbID3Tag, ByVal That As String)
        Select Case cmbPosition1.SelectedIndex
            Case Is = 0
                This.LeadArtist = That
            Case Is = 1
                This.Title = That
            Case Is = 2
                This.Album = That
            Case Is = 3
                This.Year = That
            Case Is = 4
                This.TrackPosition = That
            Case Is = 5
                This.BeatsPerMinute = That
            Case Is = 6
                This.CopyrightYear = That
            Case Is = 7
                This.CopyrightHolder = That
            Case Is = 8
                This.Genre = That
            Case Is = 9
                This.FileId = That
            Case Is = 10
                This.Comments = That
        End Select
    End Sub

    Private Sub Set2Position(ByRef This As CddbID3Tag, ByVal That As String)
        Select Case cmbPosition2.SelectedIndex
            Case Is = 1
                This.LeadArtist = That
            Case Is = 2
                This.Title = That
            Case Is = 3
                This.Album = That
            Case Is = 4
                This.Year = That
            Case Is = 5
                This.TrackPosition = That
            Case Is = 6
                This.BeatsPerMinute = That
            Case Is = 7
                This.CopyrightYear = That
            Case Is = 8
                This.CopyrightHolder = That
            Case Is = 9
                This.Genre = That
            Case Is = 10
                This.FileId = That
            Case Is = 11
                This.Comments = That
        End Select
    End Sub

    Private Sub Set3Position(ByRef This As CddbID3Tag, ByVal That As String)
        Select Case cmbPosition3.SelectedIndex
            Case Is = 1
                This.LeadArtist = That
            Case Is = 2
                This.Title = That
            Case Is = 3
                This.Album = That
            Case Is = 4
                This.Year = That
            Case Is = 5
                This.TrackPosition = That
            Case Is = 6
                This.BeatsPerMinute = That
            Case Is = 7
                This.CopyrightYear = That
            Case Is = 8
                This.CopyrightHolder = That
            Case Is = 9
                This.Genre = That
            Case Is = 10
                This.FileId = That
            Case Is = 11
                This.Comments = That
        End Select
    End Sub

    Private Sub Set4Position(ByRef This As CddbID3Tag, ByVal That As String)
        Select Case cmbPosition4.SelectedIndex
            Case Is = 1
                This.LeadArtist = That
            Case Is = 2
                This.Title = That
            Case Is = 3
                This.Album = That
            Case Is = 4
                This.Year = That
            Case Is = 5
                This.TrackPosition = That
            Case Is = 6
                This.BeatsPerMinute = That
            Case Is = 7
                This.CopyrightYear = That
            Case Is = 8
                This.CopyrightHolder = That
            Case Is = 9
                This.Genre = That
            Case Is = 10
                This.FileId = That
            Case Is = 11
                This.Comments = That
        End Select
    End Sub

    Private Sub Set5Position(ByRef This As CddbID3Tag, ByVal That As String)
        Select Case cmbPosition5.SelectedIndex
            Case Is = 1
                This.LeadArtist = That
            Case Is = 2
                This.Title = That
            Case Is = 3
                This.Album = That
            Case Is = 4
                This.Year = That
            Case Is = 5
                This.TrackPosition = That
            Case Is = 6
                This.BeatsPerMinute = That
            Case Is = 7
                This.CopyrightYear = That
            Case Is = 8
                This.CopyrightHolder = That
            Case Is = 9
                This.Genre = That
            Case Is = 10
                This.FileId = That
            Case Is = 11
                This.Comments = That
        End Select
    End Sub

    'On change, do...
    Private Sub cmbFields_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbFields.SelectedIndexChanged
        Dim Index As Integer
        Index = cmbFields.SelectedIndex
        If Index >= 1 Then
            grp2Position.Enabled = True
        Else
            grp2Position.Enabled = False
        End If
        If Index >= 2 Then
            grp3Position.Enabled = True
        Else
            grp3Position.Enabled = False
        End If
        If Index >= 3 Then
            grp4Position.Enabled = True
        Else
            grp4Position.Enabled = False
        End If
        If Index = 4 Then
            grp5Position.Enabled = True
        Else
            grp5Position.Enabled = False
        End If
    End Sub

    Private Sub cmbSeperator_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbSeperator.SelectedIndexChanged
        Dim Index As Integer
        Index = cmbSeperator.SelectedIndex
        Select Case Index
            Case Is = 0
                strSeperator = " - "
            Case Is = 1
                strSeperator = "-"
            Case Is = 2
                strSeperator = "."
            Case Is = 3
                strSeperator = "_"
        End Select
    End Sub

    Private Sub radChangeID3_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radChangeID3.CheckedChanged
        If radChangeID3.Checked = True Then
            grpFileToID3.Enabled = True
        Else
            grpFileToID3.Enabled = False
        End If
    End Sub

    Private Sub radChangeFile_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radChangeFile.CheckedChanged
        If radChangeFile.Checked = True Then
            grpID3ToFile.Enabled = True
        Else
            grpID3ToFile.Enabled = False
        End If
    End Sub

    Private Sub btnPreviewChange_Click(sender As System.Object, e As System.EventArgs) Handles btnPreviewChange.Click
        SetNewFileName()
    End Sub

    Private Sub cmbFileSeperator_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbFileSeperator.SelectedIndexChanged
        Dim Index As Integer = 0
        Dim strFilePath As String = txtFilePath.Text

        Index = cmbSeperator.SelectedIndex
        Select Case Index
            Case Is = 0
                strSeperator = " - "
            Case Is = 1
                strSeperator = "-"
            Case Is = 2
                strSeperator = "."
            Case Is = 3
                strSeperator = "_"
        End Select

    End Sub

    Private Sub chkReplaceSpace_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkReplaceSpace.CheckedChanged
        If chkReplaceSpace.Checked = True Then
            radSpaceTo_.Enabled = True
            rad_ToSpace.Enabled = True
        Else
            radSpaceTo_.Enabled = False
            rad_ToSpace.Enabled = False
        End If
    End Sub
End Class
